package synchronicity.util;

import java.util.Calendar;
import java.util.Date;
import java.util.Vector;

public class MeetingCriteria {
	private static MeetingCriteria instance;

	public static synchronized MeetingCriteria getInstance() {
		if (instance == null) {
			instance = new MeetingCriteria();
		}
		return instance;
	}

	private int startHour = 8, endHour = 17;

	private Vector acceptableDays = new Vector();

	public MeetingCriteria() {
		acceptableDays.addElement(new Integer(2));
		acceptableDays.addElement(new Integer(3));
		acceptableDays.addElement(new Integer(4));
		acceptableDays.addElement(new Integer(5));
		acceptableDays.addElement(new Integer(6));
	}

	public RangeSet getAcceptableRanges(Range range) {
		RangeSet result = new RangeSet();
		java.util.Calendar calendar = java.util.Calendar.getInstance();
		Date start = new Date(range.getLeft());
		Date end = new Date(range.getRight());
		Date current = start;

		while (current.getTime() < end.getTime()) {
			calendar.setTime(current);
			if (acceptableDays.contains(new Integer(calendar
					.get(Calendar.DAY_OF_WEEK)))) {
				long startTime, endTime;
				calendar.set(Calendar.HOUR_OF_DAY, startHour);
				startTime = calendar.getTime().getTime();
				calendar.set(Calendar.HOUR_OF_DAY, endHour);
				endTime = calendar.getTime().getTime();
				result.addRange(startTime, endTime);
			}
			current = new Date(current.getTime() + 1000 * 60 * 60 * 24);
		}
		return result;
	}
}
